var app = getApp(),
  $ = require("../../utils/httpHelper.js"),
  WxNotificationCenter = require("../../WxNotificationCenter/WxNotificationCenter.js")//通知传参
var wxParse = require("../../wxParse/wxParse.js")
Page({
  data: {
    objarr: [],
    is_fw:false,
    height: '',
    width: '',
    good_info:{},
    currentTab: 0,
    goodsinfo:{},
    isfav:true,
    goods_id:0,
    status:'',
    yhq_status:true,
    goods_gg_status: true,
    time_unit:'',
    num:1,
    speclist:[],
    speclist_array: [],
    coupon_list:[],
    activeid:'',
    sell_p:'',
    sku_id:'',
    add_cart:false,
    goods_type:'good',
    selfw_id:'',
    selfw_name:'请选择服务',
    price:'',
    market_price:'',
    stock:'',
    sell_total_num:'',
    shop_info:{},
    endtime:'',
    day:'',
    hour:'',
    min:'',
    sec:''
  },
  onShareAppMessage: function (res) {
    let member_id = app.globalData.userInfo.member_id
    return {
      title: '牛邵教育',
      path: '/pages/index/index?inviter_id=' + member_id,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onLoad: function (options) {
    console.log("onload..")

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth
        })
      }
    });
    
    if (!app.check_empty(options.id)){
      var rd_session = wx.getStorageSync('rd_session');
      this.setData({
        goods_id: options.id,
        goods_type: options.status,
      })
      let par = {
        good_id: options.id, 
        rd_session: rd_session, 
      }
      if (!app.check_empty(options.status) && options.status == 'good'){
        $.httpPost("good_detail", par, (data, err) => {
          this.setData({
            good_info: data.data.good_info,
            price: data.data.good_info.sell_price,
            isfav: data.data.good_info.is_favorite,
          });
          if (data.data.good_info.content) {
            wxParse.wxParse("about", "html", data.data.good_info.content, this)
          }
        })
      } 
      
    }
    
  },
  
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    var arr = e.detail.value.split('-')
      this.setData({
        selfw_id:arr[0],
        selfw_name:arr[1]
      })
    
  },
  topingjia(){
    wx.navigateTo({
      url: '/pages/goods/pingjia?good_id=' + this.data.goods_id
    })
  },
  clicktab(e) {
    console.log(e.target.dataset.tab)
    let tab = e.target.dataset.tab
    if (this.data.currentTab != tab) {
      this.setData({
        currentTab: tab
      })
    }
  },
  favorite(){
    if(this.data.isfav){
      var rd_session = wx.getStorageSync('rd_session');
      $.httpPost("delete_favorite_good", { good_id: this.data.goods_id, rd_session: rd_session }, (data, err) => {
        this.setData({
          isfav: data.favorite
        })
      })
    }else{
      var rd_session = wx.getStorageSync('rd_session');
      $.httpPost("add_favorite_good", { good_id: this.data.goods_id, rd_session: rd_session,types:1}, (data, err) => {
        this.setData({
          isfav: data.favorite
        })
      })
    }
    
  },
  callphone(e){
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  
  canlGG(){
    this.setData({
      goods_gg_status: true
    })
  },
  goodsGG() {
    // if (this.data.selfw_id == '' && this.data.speclist.length != 0){
    //   wx.showToast({
    //     title: '先选择服务',
    //   })
    //   return
    // }
    this.setData({
      goods_gg_status: false,
    })
  },
  addCart(){
    this.setData({
      goods_gg_status: false,
      add_cart:true
    })
  },
  //待付款页面
  obligation(){
      var rd_session = wx.getStorageSync('rd_session');
      if(this.data.add_cart){
        let par = {
            good_id: this.data.goods_id, 
            total_num: this.data.num, 
            rd_session: rd_session
        }
        $.httpPost("add_cart", par, (data, err) => {
          wx.showToast({
            title: data.message,
            duration: 2000,
          })
          this.setData({
            add_cart: false
          })
        })
      }else{
        let  par = `num=${this.data.num}&good_id=${this.data.goods_id}`
        wx.navigateTo({
          url: '/pages/obligation/obligation2?' + par
        })
      }
      this.setData({
        goods_gg_status: true,
      })
  },
  
  sub(){
    console.log(this.data.num)
    if(this.data.num == 1) return;
    this.setData({
      num:this.data.num-1
    })
  },
  add(){
    this.setData({
      num: this.data.num+1
    })
  },
  seleAct(e){
    let active_id = e.target.dataset.id
    let active_index = e.target.dataset.key
    let speclist = this.data.speclist
    let sku_id = ''
    for(let index in speclist){
      speclist[index]['active'] = false
      if (index == active_index) {
        speclist[index]['active'] = true;
        sku_id = speclist[index].id
      }
    }
      
    this.setData({
      speclist: speclist
    })
    $.httpPost("get_sku", { sku_ids: sku_id, good_id: this.data.goods_id, rd_session: wx.getStorageSync('rd_session')}, (data, err) => {
      this.setData({
        price: data.data.price,
        market_price: data.data.market_price,
        stock: data.data.stock,
        sell_total_num: data.data.sell_total_num,
        sku_id : data.data.id
      })
    })
  }
});